﻿Imports System
Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Common
Imports System.Reflection

Public Class frmCouponList

    Sub initScreen()
        Dim dtResult As New DataTable

        dtResult = findProjects(_PROJECT_ID, Nothing)

        If dtResult.Rows.Count > 0 Then
            Dim dataRow As DataRow = dtResult.Rows(0)

            txtCouponProjectName.Text = dataRow.Item("PROJECT_NAME")
            txtCouponSupervisor.Text = dataRow.Item("SUPERVISOR_NAME")

            If (USER_ROLE_WELDER = _USER_ROLE) Then
                txtCouponWelder.Text = _USER_NAME
            Else
                txtCouponWelder.Text = ""
            End If
        End If

        ''''''''''''''''''''''''''''''''''''''''''''''
        ' checkCouponAllOut
        ''''''''''''''''''''''''''''''''''''''''''''''
        If (checkCouponAllOut(_PROJECT_ID, _USER_ID) = False) Then
            btnCreateCoupon.Enabled = False
        Else
            btnCreateCoupon.Enabled = True
        End If

        ''''''''''''''''''''''''''''''''''''''''''''''
        ' Other
        ''''''''''''''''''''''''''''''''''''''''''''''
        cboCouponStatus.Text = "Approve"
        welderFindCoupon()

        refreshComboCouponNo(cboCouponNo)
        refreshComboCouponCreateDate(cboCouponDate)

        btnStartCoupon_default()
    End Sub

    Private Sub MenuItemAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_WELD_AR

        frmWeldAR.Show()
        frmWeldAR.initScreen()
        Me.Hide()
    End Sub

    Private Sub MenuItemReplaceWeld_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_REPLACE_WELD

        frmReplaceWeld.Show()
        frmReplaceWeld.initScreen()
        Me.Hide()
    End Sub

    Private Sub MenuItemWeldSeq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_WELDING_SEQ

        frmWeldingSeq.Show()
        frmWeldingSeq.initScreen()
        Me.Hide()
    End Sub

    Private Sub MenuItemLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemLogout.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_PROJECT

        frmProject.Show()
        frmProject.initScreen()
        Me.Hide()
    End Sub

    Private Sub btnCouponSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCouponSearch.Click        
        welderFindCoupon()
    End Sub

    Private Sub btnCreateCoupon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateCoupon.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_CREATE_COUPON

        frmCreateCoupon.Show()
        frmCreateCoupon.initScreen()

        Me.Hide()
    End Sub

    Private Sub btnStartCoupon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartCoupon.Click
        Dim sic As ListView.SelectedIndexCollection
        sic = lvCoupon.SelectedIndices()

        If sic.Count <> 0 Then
            _ACTION_ID = lvCoupon.Items(sic.Item(0)).Tag

            If btnStartCoupon.Text = Globals.BUTTON_LABEL_REDO Then
                _ACTION = Globals.BUTTON_LABEL_REDO

                _FROM_SCREEN_ID = _SCREEN_ID
                _SCREEN_ID = SCREEN_CREATE_COUPON

                frmCreateCoupon.Show()
                frmCreateCoupon.initScreen()
                frmCreateCoupon.editCoupon(_ACTION_ID)
            ElseIf btnStartCoupon.Text = Globals.BUTTON_LABEL_EDIT Then
                _ACTION = Globals.BUTTON_LABEL_EDIT

                _FROM_SCREEN_ID = _SCREEN_ID
                _SCREEN_ID = SCREEN_CREATE_COUPON

                frmCreateCoupon.Show()
                frmCreateCoupon.initScreen()
                frmCreateCoupon.editCoupon(_ACTION_ID)
            ElseIf btnStartCoupon.Text = Globals.BUTTON_LABEL_START Then                
                _FROM_SCREEN_ID = _SCREEN_ID
                _SCREEN_ID = SCREEN_WELD_INPUT

                frmWeldInput.Show()
                frmWeldInput.startCouponInput(_ACTION_ID)
                frmWeldInput.initScreen()

                _ACTION = Globals.BUTTON_LABEL_START
                frmWeldInput.btnWeldConfirm.Text = Globals.BUTTON_LABEL_CONFIRM
            End If

            Me.Hide()
        End If
    End Sub

    Sub btnStartCoupon_default()
        btnStartCoupon.Enabled = False
        btnStartCoupon.Text = Globals.BUTTON_LABEL_START
    End Sub

    Sub welderFindCoupon()
        Dim status As String = ""
        If (cboCouponStatus.Text = "Pending") Then
            status = Globals.COUPON_STATUS_PENDING
        ElseIf (cboCouponStatus.Text = "Approve") Then
            status = Globals.COUPON_STATUS_APPROVED
        ElseIf (cboCouponStatus.Text = "Reject") Then
            status = Globals.COUPON_STATUS_REJECTED
        ElseIf (cboCouponStatus.Text = "Complete") Then
            status = Globals.COUPON_STATUS_COMPLETED
        End If
        findCouponForListing(cboCouponDate.Text, cboCouponNo.Text, status, txtCouponBarcode.Text, cboCouponInOut.Text, lvCoupon)
    End Sub

    Private Sub lvCoupon_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvCoupon.SelectedIndexChanged
        Dim sic As ListView.SelectedIndexCollection
        sic = lvCoupon.SelectedIndices()

        btnStartCoupon_default()

        If sic.Count <> 0 Then
            Dim statusCode As String = lvCoupon.Items(sic.Item(0)).SubItems(3).Text
            If (Globals.COUPON_STATUS_APPROVED = statusCode Or Globals.COUPON_STATUS_COMPLETED = statusCode) Then
                btnStartCoupon.Enabled = True
                btnStartCoupon.Text = Globals.BUTTON_LABEL_START
            ElseIf (Globals.COUPON_STATUS_PENDING = statusCode) Then
                btnStartCoupon.Enabled = True
                btnStartCoupon.Text = Globals.BUTTON_LABEL_EDIT
            ElseIf (Globals.COUPON_STATUS_REJECTED = statusCode) Then
                btnStartCoupon.Enabled = True
                btnStartCoupon.Text = Globals.BUTTON_LABEL_REDO
            End If
        End If
    End Sub

    Private Sub MenuItemCoupon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemMenu.Click
        frmMenu.Show()
        frmMenu.initScreen()
        Me.Hide()
    End Sub
End Class